# ===========================================
# 嵌入式应用万能 Makefile (修正版) - RK3506专用
# libgpiod路径确认:
#   /home/rk/rk3506_linux6.1_sdk/buildroot/output/latest/host/arm-buildroot-linux-gnueabihf/sysroot/usr
# ===========================================

# 用户自定义配置区域 ------------------------------------
# [1] 交叉编译工具链配置
#TOOLCHAIN_PATH ?= /home/rk/rk/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin
#CROSSCOMPILE ?= $(TOOLCHAIN_PATH)/arm-buildroot-linux-gnueabihf-
TOOLCHAIN_PATH ?= /usr/bin/
CROSSCOMPILE ?= $(TOOLCHAIN_PATH)

# [2] libgpiod路径配置 (根据您确认的信息)
SYSROOT_PATH ?= $(TOOLCHAIN_PATH)/../../host/arm-buildroot-linux-gnueabihf/sysroot
#LIBGPIOD_ENABLE ?=y# 默认启用

# [3] 编译选项配置
OPTIMIZATION ?= -O
DEBUG_FLAGS  ?= -g0
WARN_FLAGS   ?= -Wall -Wextra -Wno-unused-parameter -Werror=unused-variable

# [4] 目标名称配置
TARGET ?= $(notdir $(CURDIR))

# [5] 包含目录配置
INCLUDE_DIRS += . src include
INCLUDE_FLAGS = $(patsubst %,-I%,$(INCLUDE_DIRS))

.PHONY: print-var
print-var:
	@echo "LIBGPIOD_ENABLE = $(LIBGPIOD_ENABLE)"
# ====== 关键修正开始 ======
# libgpiod包含路径设置 
$(info DEBUG: LIBGPIOD_ENABLE = "$(LIBGPIOD_ENABLE)")
ifeq ($(LIBGPIOD_ENABLE), y)
  # 头文件路径 - 指向sysroot/usr/include
  INCLUDE_FLAGS += -I$(SYSROOT_PATH)/usr/include
  # 对于gpiod可能有专用的子目录
  INCLUDE_FLAGS += -I$(SYSROOT_PATH)/usr/include/gpiod
  
  # 库文件路径 - 指向sysroot/usr/lib
  LIBGPIOD_LIB_PATH = $(SYSROOT_PATH)/usr/lib
  LDFLAGS += -L$(LIBGPIOD_LIB_PATH) -lgpiod
  
  # 添加运行时路径（可选，但推荐）
  LDFLAGS += -Wl,-rpath=$(LIBGPIOD_LIB_PATH)
  
  # 添加glib等依赖库（图片中显示必需）
  LDFLAGS += -lglib-2.0 -lgobject-2.0
endif
# ====== 关键修正结束 ======

# ====== 目录结构调整 ======
# 定义build目录
BUILD_DIR := build

# 修改后的自动构建系统 ---------------------------------
# 收集所有源文件
SRC_DIRS ?= . src
SRCS := $(shell find $(SRC_DIRS) -name '*.c' -type f)

# 在build目录中保持源文件目录结构
OBJS := $(patsubst %,$(BUILD_DIR)/%.o,$(SRCS))
DEPS := $(OBJS:.o=.d)

# 确保build目录和子目录存在
$(shell mkdir -p $(dir $(OBJS)) >/dev/null)

# 工具链定义
CC      := $(CROSSCOMPILE)gcc
LD      := $(CROSSCOMPILE)ld
AR      := $(CROSSCOMPILE)ar
SIZE    := $(CROSSCOMPILE)size
OBJDUMP := $(CROSSCOMPILE)objdump
OBJCOPY := $(CROSSCOMPILE)objcopy

# 编译选项
CFLAGS   += $(OPTIMIZATION) $(DEBUG_FLAGS) $(WARN_FLAGS) $(INCLUDE_FLAGS)
CFLAGS   += -MMD -MP  # 自动生成依赖关系

# 默认目标：构建可执行文件
.PHONY: all
all: $(TARGET) size

# 可执行文件规则
$(TARGET): $(OBJS)
	$(CC) -o $@ $^ $(LDFLAGS)

# 包含自动生成的依赖文件
-include $(DEPS)

# 模式规则：从.c文件编译成.o文件 (在build目录)
$(BUILD_DIR)/%.c.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

# 特殊目标：显示内存使用情况
.PHONY: size
size: $(TARGET)
	@echo "== 内存占用分析 =="
	$(SIZE) -t $<
	
# 新增目标：验证libgpiod路径
.PHONY: verify
verify:
	@echo "===== 路径验证 ====="
	@echo "工具链: $(CROSSCOMPILE)gcc"
	@echo "sysroot路径: $(SYSROOT_PATH)"
	@echo "libgpiod库路径: $(LIBGPIOD_LIB_PATH)"
	@echo "检查libgpiod.so是否存在:"
	@ls -la $(LIBGPIOD_LIB_PATH)/libgpiod.so*
	@echo "检查gpiod头文件是否存在:"
	@ls -la $(SYSROOT_PATH)/usr/include/gpiod.h

# 生成反汇编
disasm: $(TARGET).disasm

%.disasm: %
	$(OBJDUMP) -d $< > $@

# 生成二进制文件
%.bin: %
	$(OBJCOPY) -O binary $< $@

# 清理
.PHONY: clean
clean:
	@echo "清理构建文件..."
	rm -f $(TARGET) $(TARGET).bin $(TARGET).disasm
	rm -rf $(BUILD_DIR)
	
# 安装到开发板
.PHONY: install
install: $(TARGET)
	adb push $(TARGET) /oem

# 帮助信息
.PHONY: help
help:
	@echo "=============== RK3506应用构建系统 ==============="
	@echo "  make [all]     : 构建目标 $(TARGET) (默认)"
	@echo "  make verify    : 验证库路径配置"
	@echo "  make size      : 显示内存占用情况"
	@echo "  make disasm    : 生成反汇编文件"
	@echo "  make clean     : 清理所有构建文件"
	@echo "  make install   : 部署到开发板"
	@echo ""
	@echo "libgpiod配置:"
	@echo "  启用状态: $(LIBGPIOD_ENABLE)"
	@echo "  头文件路径: $(SYSROOT_PATH)/usr/include"
	@echo "  库文件路径: $(LIBGPIOD_LIB_PATH)"
	@echo "  中间文件目录: $(BUILD_DIR)"
	@echo "================================================"

# 设置默认目标
.DEFAULT_GOAL := all